package com.example.shirotest.study;

public class StackTest {

    public static void main(String[] args) {

       String expression = "5*20-7*2";

        Calmanage calmanage = new Calmanage();
        calmanage.setExpression(expression);
        int cal = calmanage.cal();

        System.out.printf("expression %s = %d",expression,cal);
    }

}

class Calmanage{
    //创建两个栈：
    //数据栈：存放数据
    private ArrayStack numStack = new ArrayStack(10);
    //符号栈：存放符号
    private ArrayStack operStack= new ArrayStack(10);
    //定义需要的相关变量
    private String expression = "";
    //处理多位数
    String keepNum = "";
    int index = 0; //用于扫描字符串
    int num1 = 0;
    int num2 = 0;
    int oper = 0;
    int res  = 0;
    char ch  = ' '; //将每次扫描得到的char保存到ch中

    public void setExpression(String expression) {
        this.expression = expression;
    }

    public int cal() {
        //开始循环扫描expression中的每一个字符
        while (true){
            //依次得到expression中的每一个字符
            ch =expression.substring(index,index+1).charAt(0);
            //判断ch,然后做相应的处理
            if(operStack.isOper(ch)){
                //如果是运算符

                //判断当前的符号栈是否为空
                if(!operStack.isEmpty()){
                    //如果当前的操作符的优先级小于或者等于栈顶的操作符，就需要从数栈中pop出两个数，
                    //在从符号栈中pop出一个符号，进行运算，将得到的结果如数栈，然后将当前的操作符号入符号栈
                    if(operStack.priority(ch) <= operStack.priority(operStack.peek())){
                        num1 = numStack.pop();
                        num2 = numStack.pop();
                        oper = operStack.pop();
                        res = numStack.cal(num1,num2,oper);
                        //把运算的结果入数栈
                        numStack.push(res);
                        //将当前的操作符入符号栈
                        operStack.push(ch);
                    }else{
                        //如果当前的操作符优先级大于栈中的操作符，就直接入符号栈
                        operStack.push(ch);
                    }
                }else{
                    //如果是空直接入栈
                    operStack.push(ch);
                }
            }else{
                //如果是数字，则直接入数栈
                //numStack.push(ch - 48); //ASCII转换
                //多位数处理 分析思路
                //1、处理多位数时，不能发现是一个数就立即入栈，因为可能是多位数
                //2、需要向expression表达式的index后再看一位，如果是数就进行扫描，如果是符号就入栈
                //3、我们需要定义一个变量字符串，用于拼接
                keepNum += ch;

                //如果ch已经是expression的最后一位，就直接入栈
                if(index == expression.length() - 1){
                    numStack.push(Integer.parseInt(keepNum));
                }else {

                    //判断下一个字符是不是数字，如果是数字，就继续扫描，如果是运算符，则入栈
                    if (operStack.isOper(expression.substring(index + 1, index + 2).charAt(0))) {
                        numStack.push(Integer.parseInt(keepNum));
                        keepNum = "";
                    }
                }

            }

            //让index+1,并判断是否扫描到expression最后
            index++;
            if(index >= expression.length()){
                break;
            }

        }

        //当表达式扫描完毕，就顺序的从数栈和符号栈中pop出相应的数和符号，并运行
        while (true){
            //如果符号栈为空，则计算到最后的结果，数栈中只有一个数字【结果】
            if(operStack.isEmpty()){
                break;
            }
            num1 = numStack.pop();
            num2 = numStack.pop();
            oper = operStack.pop();
            res = numStack.cal(num1,num2,oper);
            //把运算的结果入数栈
            numStack.push(res);
        }

        return numStack.pop();
    }



}

//定义一个ArrayStack 数组模拟栈
class ArrayStack{
    private int maxSize; //栈的大小
    private int[] stack; //数组，存放数据
    private int top = -1; //栈顶位置，初始值为-1

    //构造器
    public ArrayStack(int maxSize) {
        this.maxSize = maxSize;
        stack = new int[maxSize];
    }

    //返回栈顶的值
    public int peek(){
        return stack[top];
    }

    //栈满
    public boolean isFull(){
        return top == maxSize-1;
    }

    //栈空
    public boolean isEmpty(){
        return top == -1;
    }

    //入栈
    public void push(int value){
        if(isFull()){
            System.out.println("The stack is full");
            return;
        }
        top++;
        stack[top] = value;
    }

    //出栈
    public int pop(){
        if(isEmpty()){
            //抛出异常
            throw new RuntimeException("Exception: The stack is empty");
        }
        int value = stack[top];
        top--;
        return value;
    }

    //遍历栈
    public void show(){
        if(isEmpty()){
            System.out.println("The stack is empty");
            return;
        }

        for (int i = top; i >= 0; i--) {
            System.out.printf("The stack[%d] is: %d\n",i,stack[i]);
        }
    }

    //返回运算符的优先级，优先级由程序员来确定，优先级使用数字表示
    //数字越大，则优先级就越高 假定目前表达式只有 +，-，*，/
    public int priority(int oper){
        if(oper == '*' || oper == '/'){
            return 1;
        }else if (oper == '+' || oper == '-'){
            return 0;
        }else {
            return -1;
        }
    }

    //判断是不是一个运算符
    public boolean isOper(char val){
        return val == '+' || val == '-' || val == '*' || val == '/';
    }

    //计算方法
    public int cal(int num1,int num2,int oper){
        int res = 0;
        switch (oper){
            case '+':
                res = num1 + num2;
                break;
            case '-':
                res = num2 - num1;
                break;
            case '*':
                res = num1 * num2;
                break;
            case '/':
                res = num2 / num1;
                break;
            default:
                break;
        }
        return res;
    }
}
